package singlethreaded;





import javax.swing.SwingWorker;

import view.View;

import controller.MonteCarloSimulation;


public class ControllerSingleThreaded extends SwingWorker<Void, Void>{
	
	String callPutFlag;
	public double s;
	public double x;
	public double t;
	public double r;
	public double b;
	public double v;
	public int nSteps;
	public int nSimulations;
	View view;

	public ControllerSingleThreaded(final String callPutFlag, final double s, final double x, final double t,
			double r, final double b, final double v, final int nSteps,int nSimulations, final View view) {
		this.callPutFlag = callPutFlag;
		this.s=s;
		this.x=x;
		this.t=t;
		this.r=r;
		this.b=b;
		this.v=v;
		this.nSteps=nSteps;
		this.nSimulations=nSimulations;
		this.view = view;
		
		
	}

	@Override
	protected Void doInBackground() throws Exception {
		double sum =0;
		
		//start the chrono
		double start = System.currentTimeMillis();
		
		// perform the computation
		for (int i=0 ; i< nSimulations ;i++)
		{
			sum+=MonteCarloSimulation.oneSimulation(callPutFlag,s, x, t, b, v, nSteps);
			view.pb.setValue(Math.round(i*100/nSimulations));
		}
		
		//perform the average
		double result = MonteCarloSimulation.average(r, t, nSimulations, sum);
		
		
		
		//stop the chrono
		double end = System.currentTimeMillis();
		
		//display result
		System.out.println("SingleThread");
		System.out.println("callPutFlag "+callPutFlag+" s "+s+" x "+x+" t "+t+ " r "+r+
							" b "+b+" v "+v+" nSteps "+ nSteps+ " nSimulations "+nSimulations);
		view.lab.setText("Result : "+result+" in "+(end-start)/1000 + " seconds");
		view.info.setText("");
		System.out.println("Result : "+result+" in "+(end-start)/1000 + " seconds");
		view.pb.setValue(100);
		return null;
	}

	
}
